// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  // 1. 找到当前用户
  const usersDb = cloud.database().collection('users')
  const userData = await usersDb.where({
    openid
  }).get()
  const user = userData.data[0]
  delete user._id
  const newGroupList = user.groups.filter(item => event.groupId !== item.groupId)
  // 2. 从用户数据中删除该学习组
  user.groups = newGroupList
  await usersDb.where({
    openid
  }).update({
    data: user
  })
  // 3. 返回成功信息
  return {
    status: 0,
    msg: '退出成功！'
  }
}